<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\DocumentStoreRequest;
use App\Models\Document;
use App\Models\DocumentTag;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class DocumentController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Document::filter(\request()->all())
            ->with(['category'])
            ->orderByDesc('id')
            ->paginateFilter(\request('perPage', 10));

        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param DocumentStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(DocumentStoreRequest $request)
    {
        $params = $request->validated();
        $params['disabled'] = 1;

        $categoryIds = $params['categoryId'];
        $tagIds = [];
        // 标签处理
        if (!empty($params['tags'])) {
            foreach ($params['tags'] as $tag) {
                $_t = DocumentTag::where('name', $tag)->first();
                if (!$_t) $_t = DocumentTag::create(['name' => $tag]);
                $tagIds[] = $_t->id;
            }
        }
        unset($params['tags']);
        unset($params['categoryId']);

        $document = Document::create($params);
        $document->category()->sync($categoryIds);
        $document->tag()->sync($tagIds);

        return $this->restful();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return JsonResponse|object
     */
    public function show($id)
    {

        return $this->restful();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param DocumentStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(DocumentStoreRequest $request, int $id)
    {
        $params = $request->validated();
        $document = Document::findOrFail($id);

        $categoryIds = $params['categoryId'];
        $tagIds = [];
        // 标签处理
        if (!empty($params['tags'])) {
            foreach ($params['tags'] as $tag) {
                $_t = DocumentTag::where('name', $tag)->first();
                if (!$_t) $_t = DocumentTag::create(['name' => $tag]);
                $tagIds[] = $_t->id;
            }
        }
        unset($params['tags']);
        unset($params['categoryId']);

        $document->update($params);
        $document->category()->sync($categoryIds);
        $document->tag()->sync($tagIds);

        return $this->restful();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $document = Document::findOrFail($id);
        $document->delete();

        return $this->restful();
    }

    public function getTagOptions() {
        $list = DocumentTag::select(['name as value', 'name as label'])->get();

        return $this->restful($list);
    }

    public function getOptions()
    {
        $list = Document::select(['id as value', 'title as label'])->get();

        return $this->restful($list);
    }
}
